package com.rc.saas.mini.charge;

import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.common.constant.CommonConstant;
import com.rc.saas.mini.evcharge.CurrDataService;
import com.rc.saas.mini.evcharge.DataUtil;
import com.rc.saas.mini.vo.charge.CommandCurrDataReq;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CurrDataFacadeImpl implements CurrDataFacade {
    private static Logger logger = LogManager.getLogger(CurrDataFacadeImpl.class);

    @Autowired
    private CurrDataService currDataService;


    @Override
    public TResponse getCurrDataMap(CommandCurrDataReq commandCurrDataReq) {
        logger.info("getCurrDataMap 采样 CommandCurrDataReq:{}", commandCurrDataReq);

        try {
            if (commandCurrDataReq.getElecPileId() == null || StringUtils.isBlank(commandCurrDataReq.getGunNum())){
                return TResponse.FAIL(CommonConstant.MSG_PARAMETER_EMPTY);
            }

            String[] codes = DataUtil.turnCode(commandCurrDataReq.getCode());
            if (codes == null){
                return TResponse.FAIL("数据编码有误");
            }

            //调用指令
            Map<String,Object> result= currDataService.getCurrData(commandCurrDataReq.getElecPileId(),
                    commandCurrDataReq.getMemberId(), commandCurrDataReq.getGunNum(), commandCurrDataReq.getCode());

            logger.info("getCurrData:{}", result);
            return TResponse.SUCCESS_DATA(result);
        }catch (Exception e){
            logger.error("HLHTCEC.getCurrData 异常", e);
            return TResponse.FAIL(CommonConstant.MSG_EXCEPTION);
        }
    }
    @Override
    public TResponse getCurrDataSoc(CommandCurrDataReq commandCurrDataReq) {
        logger.info("getCurrDataSoc 采样 CommandCurrDataReq:{}", commandCurrDataReq);

        try {
            if (commandCurrDataReq.getElecPileId() == null || org.apache.commons.lang3.StringUtils.isBlank(commandCurrDataReq.getGunNum())) {
                return TResponse.FAIL(CommonConstant.MSG_PARAMETER_EMPTY);
            }
            String[] codes = DataUtil.turnCode(commandCurrDataReq.getCode());
            if (codes == null) {
                return TResponse.FAIL("数据编码有误");
            }

            //调用指令
            Map<String, Object> result = currDataService.getCurrSoc(commandCurrDataReq.getElecPileId(),
                    commandCurrDataReq.getGunNum(), commandCurrDataReq.getCode());

            TResponse tResponse = TResponse.SUCCESS();
            tResponse.put(result);
            return tResponse;
        } catch (Exception e) {
            logger.error("HLHTCEC.getCurrDataSoc 异常", e);
            return TResponse.FAIL(CommonConstant.MSG_EXCEPTION);
        }
    }

}
